package com.mezz.bones.im.logic.service.impl;

import com.mezz.bones.im.common.domain.user.ImUserClientInfo;
import com.mezz.bones.im.common.enums.OnlineStatusEnum;
import com.mezz.bones.im.logic.service.IUserOnlineStatusService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
/**
 * @Author: zhuang
 * @Date: 2025/6/20 10:47
 * @Description: TODO
 * @Version: 1.0
 **/

@Service
@Slf4j
@AllArgsConstructor
public class UserOnlineStatusServiceImpl implements IUserOnlineStatusService {


    @Resource
    private RedissonClient redissonClient;

    private static final String ONLINE_STATUS_KEY_PREFIX = "im:user:info:"; // 用来标识用户在线状态的键前缀

    @Override
    public boolean isOnline(String uid) {

        String mapKey = ONLINE_STATUS_KEY_PREFIX + uid; // 使用 uid 构建 Redis map 键
        RMap<String, ImUserClientInfo> statusMap = redissonClient.getMap(mapKey);
        return statusMap.values().stream().anyMatch(imUserClientInfo -> imUserClientInfo.getOnline().equals(OnlineStatusEnum.ONLINE.getCode()));
    }
}
